﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using JSC.Portal.Entities;
using System.Security.Principal;

namespace JSC.Portal.Web.Helper
{
    public static class PortalControlExtensions
    {
        private const string BUTTON_FORMAT = @"<div class='button' style='float: {0};'><div>{1}</div></div>";
        private const string SEPERATOR = @"<div class='seperator' style='float: {0}'><div></div></div>";

        public static string LinkButtonLeft(this HtmlHelper htmlHelper, string text, string href)
        {
            return LinkButton(htmlHelper, text, href, "left");          
        }

        public static string LinkButtonRight(this HtmlHelper htmlHelper, string text, string href)
        {
            return LinkButton(htmlHelper, text, href, "right");
        }

        public static string SeperatorLeft(this HtmlHelper htmlHelper)
        {
            return string.Format(SEPERATOR, "left");
        }

        public static string SeperatorRight(this HtmlHelper htmlHelper)
        {
            return string.Format(SEPERATOR, "right");
        }

        public static string SubmitButtonRight(this HtmlHelper htmlHelper, string text)
        {
            string submit = string.Format(@"<input type='submit' value='{0}' />", text);
            return string.Format(BUTTON_FORMAT, "right", submit);
        }

        public static string GridLinkButton(this HtmlHelper htmlHelper, string text, string href)
        {
            return string.Format(@"<a class='gridlinkbutton' href='{0}'>{1}</a>", href, text);
        }

        private static string LinkButton(this HtmlHelper htmlHelper, string text, string href, string position)
        {
            return string.Format(@BUTTON_FORMAT, position, string.Format(@"<span>{0}</span>", GenerateLink(htmlHelper, href, text)));
        }

        public static string GenerateLink(this HtmlHelper htmlHelper, string href, string text)
        {
            return string.Format(@"<a href='{0}'>{1}</a>", href, text);
        }

        public static string GenerateLink(this HtmlHelper htmlHelper, string href, string text, params Roles[] acceslevels)
        {
            IPrincipal p = htmlHelper.ViewContext.HttpContext.User;
            if (p != null)
            {
                bool isInRole = false;
                foreach (Roles al in acceslevels)
                    isInRole |= p.IsInRole(al.ToString());

                if (isInRole)
                    return GenerateLink(htmlHelper, href, text);
            }
            return string.Empty;
        }
    }
}
